package com.Jano.service.Impl;

import com.Jano.dao.Impl.ProductDaoImpl;
import com.Jano.dao.ProductDao;
import com.Jano.dto.*;
import com.Jano.entity.Product;
import com.Jano.exception.BusinessException;
import com.Jano.service.ProductService;
import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.Tuple;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

import static com.Jano.constant.BusinessConstant.*;

/**
 * description: ProductServiceImpl <br>
 * date: 2022/6/10 6:49 PM <br>
 * author: zoumaoji <br>
 * version: 1.0 <br>
 */
public class ProductServiceImpl extends AbstractBaseServiceImpl implements ProductService {
    ProductDao productDao = new ProductDaoImpl();

    @Override
    public void saveClickCount(Integer productId, Integer clickCount) {
        try (JedisPool jedisPool = new JedisPool(REDIS_HOST, REDIS_POST);
             Jedis jedis = jedisPool.getResource()) {
            jedis.zadd(REDIS_PRODUCT_KEY, clickCount, String.valueOf(productId));
        }
    }

    @Override
    public long updateClickCount(Integer productId, Integer clickCount) {
        try (JedisPool jedisPool = new JedisPool(REDIS_HOST, REDIS_POST);
             Jedis jedis = jedisPool.getResource()) {
            if (Objects.isNull(jedis.zscore(REDIS_PRODUCT_KEY, String.valueOf(productId)))) {
                /** 当此元素不存在时，新存储以点击量作为sortedset后面的评分 商品id作为sortedset中的元素*/
                saveClickCount(productId, clickCount);
            }
            /**对sortedset中的点击量+1*/
            return jedis.zincrby(REDIS_PRODUCT_KEY, REDIS_DEFAULT_INCREMENTAL, String.valueOf(productId)).longValue();
        }
    }

    @Override
    public BigDecimal totalPrice(Map<String, String> carts) {
        BigDecimal total = new BigDecimal("0");
        Set<String> keySet = carts.keySet();
        for (String productId : keySet) {
            ProductPrice productPrice = productDao.selectPriceByProductId(Integer.valueOf(productId));
            String count = carts.get(productId);
            BigDecimal totalCount = new BigDecimal(count);
            BigDecimal price = productPrice.getPrice();
            BigDecimal multiply = price.multiply(totalCount);
            total = multiply.add(total);
        }
        return total;
    }

    @Override
    public List<ProductCartDto> myCarts(Long myCartsNumber, Map<String, String> carts) {
        List<ProductCartDto> productCartDtoList = new ArrayList<>();
        try (JedisPool jedisPool = new JedisPool(REDIS_HOST, REDIS_POST);
             Jedis jedis = jedisPool.getResource()) {
            Set<String> keySet = carts.keySet();
            for (String productId : keySet) {
                Product product = productDao.selectProductByProductId(Integer.parseInt(productId));
                String productCount = carts.get(productId);
                ProductCartDto productCartDto = ProductCartDto.builder()
                        .productId(product.getProductId())
                        .title(product.getTitle())
                        .sellPoint(product.getSellPoint())
                        .price(product.getPrice())
                        .clickCount(product.getClickCount())
                        .image(product.getImage())
                        .cid(product.getCid())
                        .created(product.getCreated())
                        .productCount(Integer.valueOf(productCount))
                        .productLength(myCartsNumber)
                        .build();
                productCartDtoList.add(productCartDto);
            }
        }
        return productCartDtoList;
    }

    @Override
    public PageBean getProductPageInfo(Integer cid, Integer pageNo, Integer pageSize) {
        List<ProductPageRow> productPageRows = productDao.selectProductPageRows(cid, (pageNo-1)*pageSize, pageSize);
        throwIfTrue(productPageRows.isEmpty(),"暂无数据");
        PageBean pageBean = new PageBean();
        pageBean.setRows(productPageRows);
        pageBean.setPageCount((long) productPageRows.size());
        return pageBean;
    }

    @Override
    public Product getProductById(Integer productId) {
        return productDao.selectProductByProductId(productId);
    }
}
